Imports System.ComponentModel
Imports System.Web.Security

<DataObject(True)> _
Public Class MembershipODS

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Shared Function GetMembers() As List(Of MembershipUserWrapper)
        Return GetMembers(True, True, True, True, True, True, Nothing, Nothing, Nothing)
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, True)> _
    Public Shared Function GetMembers(ByVal FieldToSort As String) As List(Of MembershipUserWrapper)
        Return GetMembers(True, True, True, True, True, True, Nothing, Nothing, FieldToSort)
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Shared Function GetMembers( _
        ByVal returnAllApprovedUsers As Boolean, _
        ByVal returnAllNotApprovedUsers As Boolean, _
        ByVal returnAllOnlineUsers As Boolean, _
        ByVal returnAllNotOnlineUsers As Boolean, _
        ByVal returnAllLockedOutUsers As Boolean, _
        ByVal returnAllNotLockedOutUsers As Boolean, _
        ByVal StringToSearch As String, _
        ByVal FieldToSearch As String, _
        ByVal FieldToSort As String) As List(Of MembershipUserWrapper)

        Dim muc As MembershipUserCollection
        Dim memberList As New List(Of MembershipUserWrapper)

        ' Check if searching...
        If String.IsNullOrEmpty(StringToSearch) Then
            muc = Membership.GetAllUsers
        ElseIf FieldToSearch = "Email" Then
            muc = Membership.FindUsersByEmail(StringToSearch)
        Else
            muc = Membership.FindUsersByName(StringToSearch)
        End If

        ' Select only the one that match with critiria of LockedOut and Approved
        For Each mu As MembershipUser In muc
            If (((returnAllApprovedUsers = True) And (mu.IsApproved = True)) _
            Or ((returnAllNotApprovedUsers = True) And (mu.IsApproved = False))) _
            AndAlso (((returnAllOnlineUsers = True) And (mu.IsOnline = True)) _
            Or ((returnAllNotOnlineUsers = True) And (mu.IsOnline = False))) _
            AndAlso (((returnAllLockedOutUsers = True) And (mu.IsLockedOut = True)) _
            Or ((returnAllNotLockedOutUsers = True) And (mu.IsLockedOut = False))) Then
                Dim muw As MembershipUserWrapper = New MembershipUserWrapper(mu)
                memberList.Add(muw)
            End If
        Next

        If String.IsNullOrEmpty(FieldToSort) Then
            FieldToSort = "UserName"
        End If
        ' Make a special version of sortData for the switch statement so that whether or not the
        ' DESC is appended to the string sortData, it will switch on the base of that string.
        Dim sortDataBase As String = FieldToSort ' init and assume there is not DESC appended to sortData
        Dim descString As String = " DESC"
        If (FieldToSort.EndsWith(descString)) Then
            sortDataBase = FieldToSort.Substring(0, FieldToSort.Length - descString.Length)
        End If

        Dim comparison As Comparison(Of MembershipUserWrapper)
        Select Case sortDataBase
            Case "UserName" : comparison = New Comparison(Of MembershipUserWrapper)(AddressOf compareUserName)
            Case "Email" : comparison = New Comparison(Of MembershipUserWrapper)(AddressOf compareEmail)
            Case "CreationDate" : comparison = New Comparison(Of MembershipUserWrapper)(AddressOf compareCreationDate)
            Case "IsApproved" : comparison = New Comparison(Of MembershipUserWrapper)(AddressOf compareIsApproved)
            Case "IsOnline" : comparison = New Comparison(Of MembershipUserWrapper)(AddressOf compareIsOnline)
            Case "LastLoginDate" : comparison = New Comparison(Of MembershipUserWrapper)(AddressOf compareLastLoginDate)
            Case Else : comparison = New Comparison(Of MembershipUserWrapper)(AddressOf compareUserName)
        End Select

        If (FieldToSort.EndsWith("DESC")) Then
            memberList.Sort(comparison)
            memberList.Reverse()
        Else
            memberList.Sort(comparison)
        End If

        Return memberList
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Shared Function GetMember(ByVal userName As String) As List(Of MembershipUserWrapper)
        If String.IsNullOrEmpty(userName) Then Return Nothing
        Dim mu As MembershipUser = Membership.GetUser(userName)
        Dim memberList As New List(Of MembershipUserWrapper)
        If mu Is Nothing Then
            Return memberList
        End If
        memberList.Add(New MembershipUserWrapper(mu))
        Return memberList
    End Function

    <DataObjectMethod(DataObjectMethodType.Update, True)> _
    Public Shared Sub UpdateMember(ByVal userName As String, ByVal email As String, _
    ByVal comment As String, ByVal isApproved As Boolean, ByVal isLockedOut As Boolean)
        Dim mu As MembershipUser = Membership.GetUser(userName)
        Dim isDirty As Boolean = False
        If Not String.IsNullOrEmpty(email) Then
            If mu.Email <> email Then
                mu.Email = email
                isDirty = True
            End If
        End If
        If mu.IsApproved <> isApproved Then
            mu.IsApproved = isApproved
            isDirty = True
        End If
        If mu.Comment <> comment Then
            mu.Comment = comment
            isDirty = True
        End If
        If isDirty Then Membership.UpdateUser(mu)
        If mu.IsLockedOut AndAlso (mu.IsLockedOut <> isLockedOut) Then mu.UnlockUser()
    End Sub

#Region "Private Compare Functions"

    Private Shared Function compareUserName(ByVal lhs As MembershipUserWrapper, ByVal rhs As MembershipUserWrapper) As Integer
        Return lhs.UserName.CompareTo(rhs.UserName)
    End Function

    Private Shared Function compareEmail(ByVal lhs As MembershipUserWrapper, ByVal rhs As MembershipUserWrapper) As Integer
        If String.IsNullOrEmpty(lhs.Email) OrElse String.IsNullOrEmpty(rhs.Email) Then
            Return 0
        Else
            Return lhs.Email.CompareTo(rhs.Email)
        End If
    End Function

    Private Shared Function compareCreationDate(ByVal lhs As MembershipUserWrapper, ByVal rhs As MembershipUserWrapper) As Integer
        Return lhs.CreationDate.CompareTo(rhs.CreationDate)
    End Function

    Private Shared Function compareIsApproved(ByVal lhs As MembershipUserWrapper, ByVal rhs As MembershipUserWrapper) As Integer
        Return lhs.IsApproved.CompareTo(rhs.IsApproved)
    End Function

    Private Shared Function compareIsOnline(ByVal lhs As MembershipUserWrapper, ByVal rhs As MembershipUserWrapper) As Integer
        Return lhs.IsOnline.CompareTo(rhs.IsOnline)
    End Function

    Private Shared Function compareLastLoginDate(ByVal lhs As MembershipUserWrapper, ByVal rhs As MembershipUserWrapper) As Integer
        Return lhs.LastLoginDate.CompareTo(rhs.LastLoginDate)
    End Function

#End Region

End Class
